package com.privated.work.utils;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串实体类
 *
 * @author Leon
 * 2014-4-11 上午11:42:24
 */
public class StringUtil {

    private static final String PATTERN = "[0-9]*";

    /**
     * 分解字符串(整形)
     */
    public static List<Integer> getIntegerList(String param) {
        if (param == null || "".equals(param)) {
            return new ArrayList<>();
        }

        String[] params = param.split(",");
        List<Integer> resultList = new ArrayList<Integer>();
        for (String par : params) {
            resultList.add(Integer.parseInt(par));
        }

        return resultList;
    }

    /**
     * 分解字符串(Long)
     */
    public static List<Long> getLongList(String param) {
        if (param == null || "".equals(param)) {
            return new ArrayList<>();
        }

        String[] params = param.split(",");
        List<Long> resultList = new ArrayList<Long>();
        for (String parm : params) {
            resultList.add(Long.parseLong(parm));
        }

        return resultList;
    }

    /**
     * 构造字符串
     */
    public static String getListString(List<Integer> ids) {
        if (ids == null) {
            return null;
        }

        StringBuffer result = new StringBuffer();

        for (int i = 0, length = ids.size(); i < length; i++) {
            result.append(ids.get(i));

            if (i != (length - 1)) {
                result.append(",");
            }
        }

        return result.toString();
    }

    /**
     * 构造字符串
     */
    public static String getLongListString(List<Long> ids) {
        StringBuffer result = new StringBuffer();

        for (int i = 0, length = ids.size(); i < length; i++) {
            result.append(ids.get(i));

            if (i != (length - 1) && length > 1) {
                result.append(",");
            }
        }

        return result.toString();
    }

    /**
     * 构造字符串
     */
    public static String getListStringLong(List<Long> ids) {
        StringBuffer result = new StringBuffer();

        for (int i = 0, length = ids.size(); i < length; i++) {
            result.append(ids.get(i));

            if (i != (length - 1)) {
                result.append(",");
            }
        }

        return result.toString();
    }

    /**
     * 构造字符串
     */
    public static String getArrayListString(List<String> paramList) {
        StringBuffer result = new StringBuffer();

        for (int i = 0, length = paramList.size(); i < length; i++) {
            result.append("'" + paramList.get(i) + "'");

            if (i != (length - 1)) {
                result.append(",");
            }
        }

        return result.toString();
    }

    /**
     * 构造HTTP字符串
     */
    public static String getArrayListStringHttp(List<String> paramList) {
        StringBuffer result = new StringBuffer();

        for (int i = 0, length = paramList.size(); i < length; i++) {
            result.append(paramList.get(i));

            if (i != (length - 1)) {
                result.append(",");
            }
        }

        return result.toString();
    }

    /**
     * 分解字符串（double）
     */
    public static List<Double> getDoubleList(String param) {
        String[] params = param.split(",");
        List<Double> resultList = new ArrayList<Double>();
        for (String par : params) {
            resultList.add(Double.parseDouble(par));
        }

        return resultList;
    }

    /**
     * 获取六位随机数
     *
     * @return
     */
    public static String getAuthCode() {
        String str = "0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        List<Integer> list = new ArrayList<>();
        while (sb.length() < 6) {
            int number = random.nextInt(10);
            if (sb.length() > 0 && !"0".equals(sb.substring(0, 1))) {
                int n = 0;
                for (int i = 0; i < list.size(); i++) {
                    if (!((Object) number).equals(list.get(i))) {
                        n++;
                    }
                }
                if (n == sb.length()) {
                    sb.append(str.charAt(number));
                    list.add(number);
                }
            } else {
                if (!"0".equals(String.valueOf(str.charAt(number)))
                        && sb.length() == 0) {
                    sb.append(str.charAt(number));
                    list.add(number);
                }
            }
        }
        return sb.toString();
    }

    /**
     * 判断字符串是否为数字
     *
     * @param str
     * @return
     */
    public static boolean isNumeric(String str, Boolean isUsePattern) {
        Pattern pattern = Pattern.compile(PATTERN);
        return pattern.matcher(str).matches();
    }

    /**判断是否是数字*/
    /**
     * 判断是否是数字  支持整数 、小数、0
     */
    public static boolean isNumeric(String str) {
        int begin = 0;
        boolean once = true;
        if (str == null || str.trim().equals("")) {
            return false;
        }
        str = str.trim();
        if (str.startsWith("+") || str.startsWith("-")) {
            if (str.length() == 1) {
                return false;
            }
            begin = 1;
        }
        for (int i = begin; i < str.length(); i++) {
            if (!Character.isDigit((str.charAt(i)))) {
                if (str.charAt(i) == '.' && once) {
                    once = false;
                } else {
                    return false;
                }
            }
        }
        if (str.length() == (begin + 1) && !once) {
            return false;
        }
        return true;
    }

    /**
     * 判断是不是手机号码
     *
     * @param mobiles
     * @return
     */
    public static boolean isMobileNO(String mobiles) {
        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    /**
     * 获取uuid
     */
    public static String getUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    public static boolean isBlank(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if ((Character.isWhitespace(str.charAt(i)) == false)) {
                return false;
            }
        }
        return true;
    }

    public static boolean isNotBlank(String str) {
        return !isBlank(str);
    }

    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    /**
     * 根据给定的正则表达式返回匹配到的字符串
     *
     * @param regex
     * @param content
     * @return
     */
    public static String getMatchStr(String regex, String content) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(content);
        if (matcher.find()) {
            for (int i = 0; i <= matcher.groupCount(); i++) {
                return matcher.group(i);
            }
        }
        return null;
    }

    /**
     * .号分解字符串
     */
    public static List<String> getSpilt(String param) {
        if (param == null || "".equals(param)) {
            return new ArrayList<>();
        }
        String[] params = param.split("\\.");
        return Arrays.asList(params);
    }

    /**
     * 按/号分隔
     * @param param
     * @return
     */
    public static List<String> getSlash(String param) {
        if (param == null || "".equals(param)) {
            return new ArrayList<>();
        }
        String[] params = param.split("/");
        return Arrays.asList(params);
    }

	/**
	 * .号分隔获取尾端
	 */
    public static String getLastSpilt(String param) {
        List<String> strings = getSpilt(param);
        return strings.size() > 0 ? strings.get(strings.size() - 1) : "";
	}

    /**
     * 截取最后一段字符串
     * @param strings
     * @return
     */
	public static String getLastString(List<String> strings){
        return strings != null && strings.size() > 0 ? strings.get(strings.size() - 1) : "";
    }

    /**
     * 判断字符串中是否包含中文
     * @param str
     * 待校验字符串
     * @return 是否为中文
     * @warn 不能校验是否为中文标点符号
     */
    public static boolean isContainChinese(String str) {
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;
    }

    /**
     * 2> 根据目标容量 划分List
     * @param source
     * @param capacity 划分完成的单个List容量
     * @param <T>
     * @return
     */
    public static <T> List<List<T>> splitListBycapacity(List<T> source,int capacity){
        List<List<T>> result=new ArrayList<List<T>>();
        if (source != null){
            int size = source.size();
            if (size > 0 ){
                for (int i = 0; i < size;) {
                    List<T> value = null;
                    int end = i+capacity;
                    if (end > size){
                        end = size;
                    }
                    value = source.subList(i,end);
                    i = end;

                    result.add(value);
                }

            }else {
                result = null;
            }
        }else {
            result = null;
        }


        return result;
    }

    public static void main(String[] args) {
        List<String> strings = new ArrayList<>();
        strings.add("1");
        strings.add("2");
        strings.add("3");
        strings.add("4");
        strings.add("5");
        System.out.println("---------11111111-----");
        for (String string : strings) {
            System.out.println(string);
        }
        strings.subList(0,2).clear();
        System.out.println("---------2222222-----");
        for (String string : strings) {
            System.out.println(string);
        }
        strings.subList(0,2).clear();
        System.out.println("---------33333-----");
        for (String string : strings) {
            System.out.println(string);
        }

    }






}
